package com.nikkerball.web.renderers;

import com.nikkerball.Choice;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.MissingResourceException;

/**
 * TODO: Document class
 *
 * @author marius (created on 2010-02-16)
 */
public class ChoiceRenderer implements IChoiceRenderer<Choice> {

    // ----- class fields -----

    private static final Logger LOGGER = LoggerFactory.getLogger(ChoiceRenderer.class);

    // ----- instance fields -----

    private Component component;

    // ----- constructors -----

    public ChoiceRenderer(Component component) {
        this.component = component;
    }

    // ----- IChoiceRenderer -----

    public String getDisplayValue(Choice choice) {
        // Try and get a label - if not return the key
        String label = choice.getKey();
        try {
            label = component.getString(choice.getKey());
        } catch (MissingResourceException e) {
            LOGGER.info("MissingResourceException: {}", choice.getKey());
        }
        return label + " (" + choice.getSymbol() + ")";
    }

    public String getIdValue(Choice choice, int i) {
        return choice.toString();
    }
}
